package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.dance.domain.DancePage;
import com.ruoyi.dance.domain.bo.DanceInformationBo;
import com.ruoyi.dance.domain.vo.DanceInformationVo;
import com.ruoyi.dance.domain.vo.DancePageVo;
import com.ruoyi.dance.service.IDanceInformationService;
import com.ruoyi.dance.service.IDancePageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 页面
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Controller
@SaIgnore
public class SysIndexController {

    /**
     * 系统基础配置
     */
    private final RuoYiConfig ruoyiConfig;
    private final IDancePageService iDancePageService;
    private final IDanceInformationService iDanceInformationService;

    /**
     * 访问首页
     */
//    @GetMapping({"/manager/**", "/manager"})
//    public String manager(HttpServletRequest request) {
//        return buildRequestPage(request, "index");
//    }

    /**
     * 访问首页
     */
    @GetMapping({"/", "/index"})
    public String index(HttpServletRequest request) {
        return buildRequestPage(request, "index");
    }

    /**
     * 访问教学成果
     */
    @GetMapping("/video")
    public String video(HttpServletRequest request) {
        return buildRequestPage(request, "video");
    }

    /**
     * 访问教学成果详情
     */
    @GetMapping("/videoDetail/{id}")
    public String videoDetail(HttpServletRequest request, @PathVariable Long id) {
        return buildRequestPage(request, "videoDetail");
    }

    /**
     * 访问新闻列表
     */
    @GetMapping("/newsList")
    public String newsList(HttpServletRequest request,
                           @RequestParam(value = "limit", defaultValue = "6") int limit,
                           @RequestParam(value = "page", defaultValue = "1") int page) {

        TableDataInfo<DanceInformationVo> dataInfo = iDanceInformationService.queryPageList(DanceInformationBo.builder()
            .status("1").build(), PageQuery.builder()
            .pageSize(limit)
            .pageNum(page)
            .build());

        request.setAttribute("dataInfo", dataInfo.getRows());
        request.setAttribute("maxPage", computePage(dataInfo.getTotal(), limit));
        request.setAttribute("page", page);

        buildHotNews(request);

        return buildRequestPage(request, "newsList");
    }

    /**
     * 访问新闻列表详情
     */
    @GetMapping("/newsDetail/{id}")
    public String newsDetail(HttpServletRequest request, @PathVariable Long id) {

        buildHotNews(request);

        DanceInformationVo information = iDanceInformationService.queryById(id);

        request.setAttribute("information", information);

        return buildRequestPage(request, "newsDetail");
    }



    /**
     * 查询页面信息
     */
    public String buildRequestPage(HttpServletRequest request, String path){

        request.setAttribute("menus", iDancePageService.getMenuCache());
        request.setAttribute("path", "/" + path);

        DancePageVo pageViewByPath = iDancePageService.getPageViewByPath("/" + path);
        request.setAttribute("title", pageViewByPath.getTitle());
        request.setAttribute("description", pageViewByPath.getDescription());
        request.setAttribute("keywords", pageViewByPath.getKeywords());

        return path;
    }


    /**
     * 计算分页
     */
    public int computePage(long total, int limit){
        long maxPage = (total / limit) + 1;
        return (int) maxPage;
    }


    /**
     * 查询热门新闻
     */
    public void buildHotNews(HttpServletRequest request){
        List<DanceInformationVo> hotNews = iDanceInformationService.queryPageList(DanceInformationBo.builder()
            .status("1").build(), PageQuery.builder()
            .pageSize(7)
            .pageNum(1)
            .orderByColumn("page_view")
            .isAsc("desc")
            .build()).getRows();
        request.setAttribute("hotNews", hotNews);
    }
}
